﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace System.Web.Mvc
{
    public static class HttpContextExtensionMethods
    {

        public static TModel TryGetOrCreate<TModel>( this HttpContextBase http, Func<TModel> creator )
        {
            return http.TryGetOrCreate<TModel>( ( string )null, creator );
        }

        public static TModel TryGetOrCreate<TModel>( this HttpContextBase http, string name, Func<TModel> creator )
        {
            if ( http != null )
            {
                if ( String.IsNullOrEmpty( name ) )
                {
                    return ( TModel )http.Items.TryGetOrCreate( typeof( TModel ), () => ( object )creator() );
                }
                else
                {
                    return ( TModel )http.Items.TryGetOrCreate( name, () => ( object )creator() );
                }
            }
            return creator();
        }

    }
}